package com.mpsc.flowable.modeler.service.impl;

import com.mpsc.flowable.modeler.dto.StartProcessDto;
import com.mpsc.flowable.modeler.service.TaskProcessService;
import org.flowable.engine.HistoryService;
import org.flowable.engine.RepositoryService;
import org.flowable.engine.RuntimeService;
import org.flowable.engine.TaskService;
import org.flowable.engine.repository.ProcessDefinition;
import org.flowable.engine.runtime.ProcessInstance;
import org.flowable.task.api.Task;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

@Service("taskProcessService")
public class TaskProcessServiceImpl implements TaskProcessService {

    @Autowired
    private TaskService taskService;
    @Autowired
    private RuntimeService runtimeService;
    @Autowired
    private HistoryService historyService;
    @Autowired
    private RepositoryService repositoryService;

    @Override
    public List<Task> startProcessByKey(StartProcessDto startProcess) {
        String processKey = startProcess.getKey();
        String businessKey = startProcess.getBusinessKey();
        //根据流程编码查询最新的流程定义
        ProcessDefinition processDef = repositoryService.createProcessDefinitionQuery()
                .processDefinitionKey(processKey).latestVersion().singleResult();
        ProcessInstance processInstance = runtimeService.startProcessInstanceById(processDef.getId(), businessKey, startProcess.getVariables());
        List<Task> taskList = taskService.createTaskQuery().processInstanceId(processInstance.getId()).list();


        return taskList;
    }
}
